package accel.activities;

import accel.connection.Connector;
import accel.connection.OfflineConnector;
import accel.dataholders.Character;
import accel.movement.MovementDetector;
import accel.movement.MovementDetector.FightType;
import accel.movements.Attack;
import accel.movements.Attack.AttackType;
import accel.storage.DBManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class TrainingActivity extends Activity {

	MovementDetector movement;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fight_offline);
		Connector.setInstance(new OfflineConnector());
        movement = MovementDetector.getInstance(this);
        movement.fightType = FightType.Training;
        movement.registerSensor();
        movement.setMovementSet(Character.instance.getAttacks());
        fillWithAttacks((LinearLayout)this.findViewById(R.id.attacks));
    }
    
    @Override
	protected void onResume() {
		super.onResume();
		movement.registerSensor();
		
	}
    @Override
	protected void onPause() {
		super.onPause();
		movement.unregisterSensor();
		try
		{
			DBManager.getInstance(this).updateCharacter(Character.instance);
		}
		catch(Exception e)
		{
			Toast.makeText(this, "Failed to update character", Toast.LENGTH_SHORT).show();
		}
	}
    
    protected void fillWithAttacks(LinearLayout layout)
    {
    	Attack [] attacks = Character.instance.getAttacks().getAll();
    	TableLayout sub;
    	TableRow row;
    	TextView txt;
    	ImageView image;
    	LinearLayout.LayoutParams params;
    	for (int i = 0; i<attacks.length; i++)
    	{
    		txt = new TextView(this);
    		txt.setText(this.getString(attacks[i].name()));
    		params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    		txt.setLayoutParams(params);
    		layout.addView(txt);
    		
    		sub = new TableLayout(this);
    		row = new TableRow(this);
    		
    		txt = new TextView(this);
    		txt.setText(attacks[i].type() == AttackType.DrainsHealth ? R.string.wounds : R.string.energyDrain);
    		txt.setGravity(Gravity.CENTER);
    		row.addView(txt);
    		
    		txt = new TextView(this);
    		txt.setText(R.string.cost);
    		txt.setGravity(Gravity.CENTER);
    		row.addView(txt);
    		
    		txt = new TextView(this);
    		txt.setText(R.string.equation);
    		txt.setGravity(Gravity.CENTER);
    		row.addView(txt);
    		
    		sub.addView(row);
    		row = new TableRow(this);
    		////////////////////////////////////////////second row
    		txt = new TextView(this);
    		txt.setText(Integer.toString(attacks[i].attack()));
    		txt.setGravity(Gravity.CENTER);
    		row.addView(txt);
    		
    		txt = new TextView(this);
    		txt.setText(Integer.toString(attacks[i].energy()));
    		txt.setGravity(Gravity.CENTER);
    		row.addView(txt);
    		
    		txt = new TextView(this);
    		txt.setText(attacks[i].equation());
    		txt.setGravity(Gravity.CENTER);
    		row.addView(txt);
    		
    		sub.addView(row);
    		sub.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    		sub.setColumnStretchable(0, true);
    		sub.setColumnStretchable(1, true);
    		sub.setColumnStretchable(2, true);
    		((TableLayout.LayoutParams)sub.getLayoutParams()).bottomMargin = 10;
    		layout.addView(sub);
    		
    		image = new ImageView(this);
    		image.setImageResource(attacks[i].instructionImage());
    		
    		layout.addView(image);
    	}
    }
}
